#include <iostream>
using namespace std;
/*
思路：由先序和后序创建二叉树
先序遍历的作为树根，将后序序列分成了两段
*/


/*
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

7 4 2 8 9 5 6 3 1
*/

const int N = 1000;
int pre[N], in[N], after[N];

struct node{
    int val;
    node *l, *r;
    node(int val = 0, node *l = NULL, node *r = NULL):val(val), l(l), r(r){}
};
int cur = 0;
void afterorder(node* p){
    if(p == NULL) return;
    afterorder(p->l);
    afterorder(p->r);
    after[cur++] = p->val;
}

int t = 0;
//建树
void buildtree(node* &root, int left, int right){
    int index = -1;
    //先序的第一个数是根，找到对应的中序的位置
    //左闭右开
    for(int i = left; i < right; i++){
        if(pre[t] == in[i]){
            index = i;
            break;
        }
    }
    if(index == -1) return;//结束
    t++;
    root = new node(in[index]);//新建节点

    if(index > left){
        buildtree(root->l, left, index);
    }
    if(index < right){
        buildtree(root->r, index + 1, right);
    }
}
void remove(node* &root){
    if(root == NULL) return;
    remove(root->l);
    remove(root->r);
    delete root;
}

int main(){
    int n;
    cin>>n;
    for(int i = 0; i < n; i++) cin>>pre[i];
    for(int i = 0; i < n; i++) cin>>in[i];
    node* root;
    //根据前序和中序建立二叉树
    buildtree(root, 0, n);
    //后序遍历
    afterorder(root);
    for(int i = 0; i < n; i++){
        cout<<after[i]<<" ";
    }
    remove(root);
    return 0;
}